package com.lujieni.netty.c4;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;

public class TestPipeline {

    public static void main(String[] args) {

        //写一个netty的ServerBootstrap代码
        new ServerBootstrap()
                .group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        // add handler head h1 -> h2 -> h3 -> h4 -> h5-> h6 tail
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("h1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h1");
                                ByteBuf bb = (ByteBuf)msg;
                                super.channelRead(ctx, bb.toString(StandardCharsets.UTF_8));
                            }
                        });
                        pipeline.addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h2");
                                super.channelRead(ctx, new Student((String) msg));
                            }
                        });
                        pipeline.addLast("h3", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h3");
                                System.out.println(msg);
                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("h".getBytes())); //invoke outbound handler from tail to head
                                //ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("h".getBytes())); // invoke outbound handler from h3 to head
                                //super.channelRead(ctx, msg); it is the end of InboundHandler, so no need invoke super.channelRead
                            }
                        });

                        pipeline.addLast("h4", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h4");
                                super.write(ctx, msg, promise);
                            }
                        });

                        pipeline.addLast("h5", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h5");
                                super.write(ctx, msg, promise);
                            }
                        });

                        pipeline.addLast("h6", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h6");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })
                .bind(8081);


    }


}

class Student{
    private String name;

    public Student(String name) {
        this.name = name;
    }

    // 生成toString方法并打印参数
    @Override
    public String toString() {
        return "Student{" +
                "name=" + name +"}";
    }

}
